package com.yangluyao.easyexcel.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.yangluyao.easyexcel.mapper.EasyExcelMapper;
import com.yangluyao.easyexcel.model.EasyExcelModel;
import com.yangluyao.easyexcel.read.listener.EasyExcelReadListener;
import com.yangluyao.easyexcel.service.IEasyExcelService;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;

/*
* @Description: TODO
* @Author:  hejiayao
* @CreateDate:  2021/7/2 16:00
* @UpdateUser:  hejiayao
* @UpdateDate:  2021/7/2 16:00
* @className: EasyExcelController
* @Package : com.yangluyao.easyexcel.controller
* @Version: 1.0
*/
@Controller
public class EasyExcelController {
    @Autowired
    private IEasyExcelService iEasyExcelService;

    @Autowired
    private EasyExcelMapper easyExcelMapper;

    /**
     * @Author hejiayao
     * @Description //TODO 动态创建模板
     * @method tempalte
     * @Date 14:48 2021/7/2
     * @param response
     * @return void
     **/
    @GetMapping("/template")
    public void tempalte(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("数据上传模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        //需要合并的列
        int[] mergeColumeIndex = {0, 0};
        //从第二行后开始合并
        int mergeRowIndex = 0;
        // 调用合并单元格工具类，此工具类是根据工程名称相同则合并后面数据
        ExcelMergeUtil excelFillCellMergeStrategy = new ExcelMergeUtil(mergeRowIndex,mergeColumeIndex);
        // 这里需要设置不关闭流
//        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//        //设置背景颜色
//        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
//        //设置头字体
//        WriteFont headWriteFont = new WriteFont();
//        headWriteFont.setFontHeightInPoints((short)13);
//        headWriteFont.setBold(true);
//        headWriteCellStyle.setWriteFont(headWriteFont);
//        //设置头居中
//        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//        //内容策略
//        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//        //设置 水平居中
//        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
//                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(),EasyExcelModel.class )
                .registerWriteHandler(excelFillCellMergeStrategy).sheet("数据上传模板").doWrite(new ArrayList<EasyExcelModel>());
    }



    /**
     * @Author hejiayao
     * @Description //TODO 导出
     * @method download
     * @Date 14:49 2021/7/2
     * @param response
     * @return void
     **/
    @GetMapping("/export")
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("数据表格下载", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //需要合并的列
        int[] mergeColumeIndex = {0, 0};
        //从第二行后开始合并
        int mergeRowIndex = 0;
        // 调用合并单元格工具类，此工具类是根据工程名称相同则合并后面数据
        ExcelMergeUtil excelFillCellMergeStrategy = new ExcelMergeUtil(mergeRowIndex,mergeColumeIndex);
        EasyExcel.write(response.getOutputStream(), EasyExcelModel.class)
                .registerWriteHandler(excelFillCellMergeStrategy).sheet("数据表格").doWrite(iEasyExcelService.list());
    }
    

    /**
     * @Author hejiayao
     * @Description //TODO 导入
     * @method upload
     * @Date 14:49 2021/7/2
     * @param file
     * @return java.lang.String
     **/
    @PostMapping("/import")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),EasyExcelModel.class , new EasyExcelReadListener(easyExcelMapper)).sheet().doRead();
        return "success";
    }

}
